package com.anche.cywebnew.Controller.systemManager;

import cn.hutool.core.util.StrUtil;
import com.anche.cywebnew.DTO.SelectDTO;
import com.anche.cywebnew.Pojo.systemManager.SysOrganization;
import com.anche.cywebnew.Pojo.systemManager.SysUser;
import com.anche.cywebnew.Service.systemManager.OrganService;
import com.anche.cywebnew.Utils.Encryption.AuthorizeUtil;
import com.anche.cywebnew.Utils.annotation.OptLog;
import com.anche.cywebnew.Utils.enums.OptType;
import com.anche.cywebnew.Utils.result.ResultUtil;
import com.github.pagehelper.Page;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/system/organization")
@Controller
public class OrganController {

    @Autowired
    private OrganService organService;

    private static final Logger logger = LoggerFactory.getLogger(OrganController.class);


    @RequestMapping("/manager")
    public String toOrganPage() {
        return "/systemManager/organ";
    }

    @GetMapping(value = "/organList")
    @ResponseBody
    public Object getOrganList(@RequestParam Map<String, Object> map) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        try {
            List<SysOrganization> sysOrganizations = null;
            List<String> cyqxhs = null;
            if("1".equals(map.get("islimit")) && sysUser.getUsertype() != 0){  // islimit为1：限制当前用户只能看对应的站点信息
                cyqxhs = organService.getListByCyqxh(sysUser.getOrgan());   // 获取当前用户所属部门以及以下子部门的序号
            }

            sysOrganizations = organService.getOrganList(map, cyqxhs);
            return ResultUtil.success(sysOrganizations);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @GetMapping(value = "/organListPage")
    @ResponseBody
    public Object getOrganListPage(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, SysOrganization sysOrganization) {
        try {
            Page<SysOrganization> pageInfo = organService.getOrganListPage(page, limit, sysOrganization);
            return ResultUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/organInfo", method = RequestMethod.PUT)
    @ResponseBody
    @OptLog(title = "更新查验机构信息", type = OptType.UPDATE)
    public Object updateOrgan(SysOrganization sysOrganization) {
        try {
            AuthorizeUtil authorizeUtil = new AuthorizeUtil();
            if (authorizeUtil.doAuthorize(sysOrganization)) {
                logger.info("更新机构[{}]成功", sysOrganization.getName());
                organService.updateOrgan(sysOrganization);
                return ResultUtil.success("机构更新成功！");
            } else {
                logger.error("<更新[{}]机构异常>:[{}]", sysOrganization.getName(), "授权码错误！");
                return ResultUtil.error(0, "授权码错误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<更新[{}]机构异常>:[{}]", sysOrganization.getName(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }


    @RequestMapping(value = "/organInfo", method = RequestMethod.POST)
    @ResponseBody
    @OptLog(title = "添加查验机构信息", type = OptType.ADD)
    public Object addOrgan(SysOrganization sysOrganization) {
        try {
            AuthorizeUtil authorizeUtil = new AuthorizeUtil();
            if (authorizeUtil.doAuthorize(sysOrganization)) {
                organService.addOrgan(sysOrganization);
                logger.info("新增机构[{}]成功", sysOrganization.getName());
                return ResultUtil.success("添加成功！");
            } else {
                logger.error("<新增[{}]机构异常>:[{}]", sysOrganization.getName(), "授权码错误！");
                return ResultUtil.error(0, "授权码错误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<新增[{}]机构异常>:[{}]", sysOrganization.getName(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/organInfo", method = RequestMethod.DELETE)
    @ResponseBody
    @OptLog(title = "删除查验机构信息", type = OptType.DEL)
    public Object delOrgan(@RequestParam("cyqxh") String cyqxh) {
        try {
            organService.delOrganByCyqxh(cyqxh);
            logger.info("<删除查验区序号为[{}]的机构成功>", cyqxh);
            return ResultUtil.success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<删除查验区序号[{}]为的机构异常>:{}", cyqxh, e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @GetMapping("/treeInfo")
    @ResponseBody
    public Object getTreeInfo(@RequestParam Map<String, Object> map) {
        try {
            SysUser currUser = (SysUser) SecurityUtils.getSubject().getPrincipal();   // 获取当前登录用户信息
            String oran = null;
            if("1".equals(map.get("islimit")) && currUser.getUsertype() != 0){  // islimit为1：限制当前用户只能看对应的站点信息
                oran = currUser.getOrgan();
            }

            List<Object> list = organService.getTreeOrganization(oran);
            logger.info("<获取组织机构树成功>");
            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<获取组织机构树异常>:{}", e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @GetMapping("/name")
    @ResponseBody
    public Object getOrganName(@RequestParam("cyqxh") String cyqxh) {
        try {
            Object name = organService.getNameByCyqxh(cyqxh);
            return ResultUtil.success(name);
        } catch (Exception e) {
            return ResultUtil.error(0, e.getMessage());
        }
    }
}
